package ArrayAndString;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class String_5 {
    //字符串匹配算法（暴力解法）
    public static int violenceMatch(String str1, String str2) {
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();
        int str1Len = str1.length();
        int str2Len = str2.length();
        int i = 0;
        int j = 0;
        while (i < str1Len && j < str2Len) {
            if (s1[i] == s2[j]) {
                ++i;
                ++j;
            } else {
                //重置i的开始位置
                i = i - j + 1;
                //子串从头开始
                j = 0;
            }
        }
        if (j == str2Len) {
            return i - j;
        } else {
            return -1;
        }
    }

    public static void main(String[] args) {
        String str1 = "ababac";
        String str2 = "abac";
        int i = violenceMatch(str1, str2);
        System.out.println(i);
        Map<String,Object> map = new HashMap<>();
        map.put("key",1);
        map.put("key",2);
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key+""+value);
        }

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("key");
        stringBuilder.append(":value");
        stringBuilder.append("\r\n");
        stringBuilder.append("key1");
        stringBuilder.append(":value1");
        stringBuilder.toString();
        System.out.println(stringBuilder.toString());
    }
}
